package com.aim.questionnaire.controller;

import com.aim.questionnaire.beans.HttpResponseEntity;
import com.aim.questionnaire.common.Constans;
import com.aim.questionnaire.dao.entity.ProjectEntity;
import com.aim.questionnaire.dao.entity.QuestionnaireEntity;

import com.aim.questionnaire.service.QuestionnaireService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.crypto.hash.Hash;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class QuestionnaireController {

  private final Logger logger = LoggerFactory.getLogger(QuestionnaireController.class);
  @Autowired
  QuestionnaireService questionnaireService;

  /**
   * 根据问卷名查询所有问卷
   *
   * @param map
   * @return
   */
  @RequestMapping(value = "/queryAllQuestionnaire", method = RequestMethod.POST,
    headers = "Accept=application/json")
  public HttpResponseEntity queryAllQuestionnaire(@RequestBody Map<String, Object> map) {
    HttpResponseEntity responseEntity = new HttpResponseEntity();
    try {
      PageInfo questionnaireEntityList = questionnaireService.queryQuestionnaireInfo(map);
      if (questionnaireEntityList.getList().size() != 0) {
        responseEntity.setCode(Constans.SUCCESS_CODE);
        responseEntity.setData(questionnaireEntityList);
        responseEntity.setMessage(Constans.STATUS_MESSAGE);
      } else {
        responseEntity.setCode(Constans.EXIST_CODE);
        responseEntity.setData(null);
        responseEntity.setMessage(Constans.QUERYFAIL_MESSAGE);
      }
    } catch (Exception e) {
      logger.error("查询所有问卷异常>>>>>>>>>>>", e);
      responseEntity.setCode(Constans.EXIST_CODE);
      responseEntity.setMessage(Constans.EXIST_MESSAGE);
    }
    return responseEntity;
  }

  /**
   * 根据id查询问卷详细信息
   */
  //queryQuestionnaireAll
  @RequestMapping(value = "/queryQuestionnaireAll", method = RequestMethod.POST,
    headers = "Accept=application/json")
  public HttpResponseEntity queryQuestionnaireAll(@RequestBody HashMap<String, Object> map) {
    HttpResponseEntity responseEntity = new HttpResponseEntity();
    try {
      Map<String,Object> questionnaire = questionnaireService.queryQuestionnaireById(map);
      if (questionnaire != null) {
        responseEntity.setCode(Constans.SUCCESS_CODE);
        responseEntity.setData(questionnaire);
        responseEntity.setMessage(Constans.STATUS_MESSAGE);
      } else {
        responseEntity.setCode(Constans.EXIST_CODE);
        responseEntity.setData(null);
        responseEntity.setMessage(Constans.QUERYFAIL_MESSAGE);
      }
    } catch (Exception e) {
      logger.error("查询问卷详细信息异常>>>>>>>>>>>", e);
      responseEntity.setCode(Constans.EXIST_CODE);
      responseEntity.setMessage(Constans.EXIST_MESSAGE);
    }
    return responseEntity;
  }

  //modifyQuestionnaire
  //修改问卷详细信息（包括题目）
  @RequestMapping(value = "/modifyQuestionnaire", method = RequestMethod.POST,
    headers = "Accept=application/json")
  public HttpResponseEntity modifyQuestionnaire(@RequestBody HashMap<String, Object> map) {
    HttpResponseEntity responseEntity = new HttpResponseEntity();
    try {
      int i = questionnaireService.modifyQuestionnaire(map);
      if (i == 1) {
        responseEntity.setCode(Constans.SUCCESS_CODE);
        responseEntity.setMessage("修改成功");
      } else {
        responseEntity.setCode(Constans.EXIST_CODE);
        responseEntity.setMessage(Constans.EXIST_MESSAGE);
      }
    } catch (Exception e) {
      logger.error("修改问卷详细信息异常>>>>>>>>>>>", e);
      responseEntity.setCode(Constans.EXIST_CODE);
      responseEntity.setMessage(Constans.EXIST_MESSAGE);
    }
    return responseEntity;
  }

  ///queryQuestionnaireById
  //查询
  @RequestMapping(value = "/queryQuestionnaireById", method = RequestMethod.POST,
    headers = "Accept=application/json")
  public HttpResponseEntity queryQuestionnaireById(@RequestBody HashMap<String, Object> map) {
    HttpResponseEntity responseEntity = new HttpResponseEntity();
    try {
      Map<String,Object> questionnaire = questionnaireService.queryQuestionnaireById(map);
      if (questionnaire != null) {
        responseEntity.setCode(Constans.SUCCESS_CODE);
        responseEntity.setData(questionnaire);
        responseEntity.setMessage(Constans.STATUS_MESSAGE);
      } else {
        responseEntity.setCode(Constans.EXIST_CODE);
        responseEntity.setData(null);
        responseEntity.setMessage(Constans.QUERYFAIL_MESSAGE);
      }
    } catch (Exception e) {
      logger.error("查询问卷详细信息异常>>>>>>>>>>>", e);
      responseEntity.setCode(Constans.EXIST_CODE);
      responseEntity.setMessage(Constans.EXIST_MESSAGE);
    }
    return responseEntity;
  }

  /**
   * 根据问卷名查询和项目id所有问卷不是本项目的问卷
   *
   * @param map
   * @return
   */
  @RequestMapping(value = "/queryQuestionnaireByNameAndProjectId", method = RequestMethod.POST,
    headers = "Accept=application/json")
  public HttpResponseEntity queryQuestionnaireByNameAndProjectId(@RequestBody Map<String, Object> map) {
    HttpResponseEntity responseEntity = new HttpResponseEntity();
    try {
      PageInfo questionnaireEntityList = questionnaireService.queryQuestionnaireByNameAndProjectid(map);
      if (questionnaireEntityList.getList().size() != 0) {
        responseEntity.setCode(Constans.SUCCESS_CODE);
        responseEntity.setData(questionnaireEntityList);
        responseEntity.setMessage(Constans.STATUS_MESSAGE);
      } else {
        responseEntity.setCode(Constans.EXIST_CODE);
        responseEntity.setData(null);
        responseEntity.setMessage(Constans.QUERYFAIL_MESSAGE);
      }
    } catch (Exception e) {
      logger.error("查询所有问卷异常>>>>>>>>>>>", e);
      responseEntity.setCode(Constans.EXIST_CODE);
      responseEntity.setMessage(Constans.EXIST_MESSAGE);
    }
    return responseEntity;
  }

  /**
   * 根据项目id查询对应问卷的信息
   *
   * @param map
   * @return
   */
  @RequestMapping(value = "/queryQuestionnaireListByProjectId", method = RequestMethod.POST,
    headers = "Accept=application/json")
  public HttpResponseEntity queryProjectList(@RequestBody Map<String, Object> map) {
    HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
    List<Map<String, Object>> list = questionnaireService.queryQuestionnaireListById(map);
    try {
      httpResponseEntity.setCode(Constans.SUCCESS_CODE);
      httpResponseEntity.setData(list);
      httpResponseEntity.setMessage(Constans.STATUS_MESSAGE);
    } catch (Exception e) {
      logger.info("addUserInfo 根据项目id查询问卷的基本信息>>>>>>>>>>>" + e.getLocalizedMessage());
      httpResponseEntity.setCode(Constans.EXIST_CODE);
      httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
    }
    return httpResponseEntity;
  }

  /**
   * 查询问卷状态
   *
   * @param questionnaireEntity
   * @return
   */
  @RequestMapping(value = "/selectQuestionnaireStatus", method = RequestMethod.POST,
    headers = "Accept=application/json")
  public HttpResponseEntity selectQuestionnaireStatus(
    @RequestBody QuestionnaireEntity questionnaireEntity) {
    HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
    int status = Integer.parseInt(
      questionnaireService.queryQuestionnaireIsStopStatus(questionnaireEntity.getId()));
    try {
      httpResponseEntity.setCode(Constans.SUCCESS_CODE);
      httpResponseEntity.setData(status);
    } catch (Exception e) {
      logger.info("addUserInfo 查询问卷状态的基本信息>>>>>>>>>>>" + e.getLocalizedMessage());
      httpResponseEntity.setCode(Constans.LOGOUT_NO_CODE);
      httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
    }
    return httpResponseEntity;
  }

  /**
   * 取消问卷和项目的关联
   *
   * @param questionnaireEntity
   * @return
   */
  @RequestMapping(value = "/cancelQuestionnaireProject", method = RequestMethod.POST,
    headers = "Accept=application/json")
  public HttpResponseEntity cancelQuestionnaireProject(
    @RequestBody QuestionnaireEntity questionnaireEntity) {
    HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
    String questionnaireId = questionnaireEntity.getId();
    int status = questionnaireService.cancelQuestionnaireProject(questionnaireId);
    try {
      if (status == 1) {
        httpResponseEntity.setCode(Constans.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constans.CANCEL_PROJECT_MESSAGE);
      } else if (status == 0) {
        httpResponseEntity.setCode(Constans.EXIST_CODE);
        httpResponseEntity.setMessage(Constans.COPY_EXIT_UPDATE_MESSAGE);
      }
    } catch (Exception e) {
      logger.info("addUserInfo 取消问卷和项目的关联>>>>>>>>>>>" + e.getLocalizedMessage());
      httpResponseEntity.setCode(Constans.LOGOUT_NO_CODE);
      httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
    }
    return httpResponseEntity;
  }

  /**
   * 建立问卷和项目的关联
   */
  @RequestMapping(value = "/addQuestionnaireProject", method = RequestMethod.POST,
    headers = "Accept=application/json")
  public HttpResponseEntity addQuestionnaireProject(@RequestBody Map<String, Object> map) {
    HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
    int status = questionnaireService.addQuestionnaireProject(map);
    try {
      if (status == 1) {
        httpResponseEntity.setCode(Constans.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constans.ADD_MESSAGE);
      } else {
        httpResponseEntity.setCode(Constans.EXIST_CODE);
        httpResponseEntity.setMessage(Constans.COPY_EXIT_UPDATE_MESSAGE);
      }
    } catch (Exception e) {
      logger.info("addUserInfo 建立问卷和项目的关联>>>>>>>>>>>" + e.getLocalizedMessage());
      httpResponseEntity.setCode(Constans.LOGOUT_NO_CODE);
      httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
    }
    return httpResponseEntity;
  }

  /**
   * 根据问卷id删除问卷
   *
   * @param questionnaireEntity
   * @return
   */
  @RequestMapping(value = "/deleteQuestionnaireById", method = RequestMethod.POST,
    headers = "Accept=application/json")
  public HttpResponseEntity deleteQuestionnaireById(
    @RequestBody QuestionnaireEntity questionnaireEntity) {
    HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
    String questionnaireId = questionnaireEntity.getId();
    int status = questionnaireService.deleteQuestionnaireInfo(questionnaireId);
    try {
      if (status == 1) {
        httpResponseEntity.setCode(Constans.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constans.DELETE_MESSAGE);
      } else if (status == 0) {
        httpResponseEntity.setCode(Constans.EXIST_CODE);
        httpResponseEntity.setMessage(Constans.COPY_EXIT_DELETE_MESSAGE);
      }
    } catch (Exception e) {
      logger.info("addUserInfo 根据问卷id删除问卷>>>>>>>>>>>" + e.getLocalizedMessage());
      httpResponseEntity.setCode(Constans.LOGOUT_NO_CODE);
      httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
    }
    return httpResponseEntity;
  }

  /**
   * 根据id修改问卷状态
   *
   * @return
   */
  @RequestMapping(value = "/modifyQuestionnaireStatus", method = RequestMethod.POST,
    headers = "Accept=application/json")
  public HttpResponseEntity modifyQuestionnaireStatus(@RequestBody HashMap<String, Object> map) {
    HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
    String questionnaireId = (String) map.get("id");
    int status = questionnaireService.modifyQuestionnaireStatus(map);
    try {
      if (status == 1) {
        httpResponseEntity.setCode(Constans.SUCCESS_CODE);
        //httpResponseEntity.setData(status);
        httpResponseEntity.setMessage(Constans.UPDATE_STATUS_MESSAGE);
      }
    } catch (Exception e) {
      logger.info("addUserInfo 根据问卷id删除问卷>>>>>>>>>>>" + e.getLocalizedMessage());
      httpResponseEntity.setCode(Constans.LOGOUT_NO_CODE);
      httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
    }
    return httpResponseEntity;
  }

  /**
   * 添加问卷
   */
  @RequestMapping(value = "/addQuestionnaire", method = RequestMethod.POST,
    headers = "Accept=application/json")
  public HttpResponseEntity addQuestionnaire(@RequestBody Map<String, Object> map) {
    HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
    int status = questionnaireService.addQuestionnaireInfo(map);
    try {
      if (status == 1) {
        httpResponseEntity.setCode(Constans.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constans.ADD_MESSAGE);
      } else if (status == 0) {
        httpResponseEntity.setCode(Constans.EXIST_CODE);
        httpResponseEntity.setMessage(Constans.NAME_EXIT_MESSAGE);
      } else {
        httpResponseEntity.setCode(Constans.EXIST_CODE);
        httpResponseEntity.setMessage(Constans.COPY_EXIT_UPDATE_MESSAGE);
      }
    } catch (Exception e) {
      logger.info("addQuestionnaireInfo 添加问卷>>>>>>>>>>>" + e.getLocalizedMessage());
      httpResponseEntity.setCode(Constans.LOGOUT_NO_CODE);
      httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
    }
    return httpResponseEntity;
  }

  /**
   * 根据问卷id修改问卷
   *
   * @param map
   * @return
   */
  @RequestMapping(value = "/modifyQuestionnaireInfo", method = RequestMethod.POST,
    headers = "Accept=application/json")
  public HttpResponseEntity modifyQuestionnaireById(@RequestBody HashMap<String, Object> map) {
    HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
    int status = questionnaireService.modifyQuestionnaireInfo(map);
    try {
      if (status == 1) {
        httpResponseEntity.setCode(Constans.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constans.UPDATE_MESSAGE);
      } else if (status == 0) {
        httpResponseEntity.setCode(Constans.EXIST_CODE);
        httpResponseEntity.setMessage(Constans.NAME_EXIT_MESSAGE);
      }
    } catch (Exception e) {
      logger.info("modifyQuestionnaireInfo 根据问卷id修改问卷>>>>>>>>>>>" + e.getLocalizedMessage());
      httpResponseEntity.setCode(Constans.LOGOUT_NO_CODE);
      httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
    }
    return httpResponseEntity;
  }

  /**
   * 根据问卷id查询问卷结束语和context
   *
   * @param map
   * @return
   */
  @RequestMapping(value = "/queryQuestContextEnd", method = RequestMethod.POST,
    headers = "Accept=application/json")
  public HttpResponseEntity getQuestionnaireById(@RequestBody HashMap<String, Object> map) {
    HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
    Map<String, Object> questionnaire = questionnaireService.queryQuestionnaireEndAndSmsContent(
      map);
    try {
      httpResponseEntity.setCode(Constans.SUCCESS_CODE);
      httpResponseEntity.setData(questionnaire);
      httpResponseEntity.setMessage(Constans.STATUS_MESSAGE);
    } catch (Exception e) {
      logger.info(
        "getQuestionnaireById 根据问卷id查询问卷结束语和context>>>>>>>>>>>" + e.getLocalizedMessage());
      httpResponseEntity.setCode(Constans.LOGOUT_NO_CODE);
      httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
    }
    return httpResponseEntity;
  }

  /**
   * 添加发送问卷方式
   */
  //addSendQuestionnaire
  @RequestMapping(value = "/addSendQuestionnaire", method = RequestMethod.POST,
    headers = "Accept=application/json")
  public HttpResponseEntity addSendQuestionnaire(@RequestBody HashMap<String, Object> map) {
    HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
    int status = questionnaireService.addSendQuestionnaire(map);
    try {
      if (status == 1) {
        httpResponseEntity.setCode(Constans.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constans.ADD_MESSAGE);
      } else if (status == 0) {
        // 邮箱格式错误
        httpResponseEntity.setCode(Constans.EXIST_CODE);
        httpResponseEntity.setMessage(Constans.EMAIL_FORMAT_MESSAGE);
      } else if (status == 2) {
        httpResponseEntity.setCode(Constans.EXIST_CODE);
        httpResponseEntity.setMessage(Constans.RELEASE_TIME_MESSAGE);
      } else {
        httpResponseEntity.setCode(Constans.EXIST_CODE);
        httpResponseEntity.setMessage(Constans.COPY_EXIT_UPDATE_MESSAGE);
      }
    } catch (Exception e) {
      logger.info("addSendQuestionnaire 添加发送问卷方式>>>>>>>>>>>" + e.getLocalizedMessage());
      httpResponseEntity.setCode(Constans.LOGOUT_NO_CODE);
      httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
    }
    return httpResponseEntity;
  }

  /**
   * 保存问卷结束语和短信内容
   *
   * @param map
   * @return
   */
  //saveSendQuestionnaire
  @RequestMapping(value = "/saveSendQuestionnaire", method = RequestMethod.POST,
    headers = "Accept=application/json")
  public HttpResponseEntity saveSendQuestionnaire(@RequestBody HashMap<String, Object> map) {
    HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
    int status = questionnaireService.saveSendQuestionnaire(map);
    try {
      if (status == 1) {
        httpResponseEntity.setCode(Constans.SUCCESS_CODE);
        httpResponseEntity.setMessage(Constans.ADD_MESSAGE);
      } else {
        httpResponseEntity.setCode(Constans.EXIST_CODE);
        httpResponseEntity.setMessage(Constans.COPY_EXIT_UPDATE_MESSAGE);
      }
    } catch (Exception e) {
      logger.info("saveSendQuestionnaire 保存问卷结束语和短信内容>>>>>>>>>>>" + e.getLocalizedMessage());
      httpResponseEntity.setCode(Constans.LOGOUT_NO_CODE);
      httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
    }
    return httpResponseEntity;
  }

  //查询所有历史问卷
  @RequestMapping(value = "/queryHistoryQuestionnaire", method = RequestMethod.POST,
    headers = "Accept=application/json")
  public HttpResponseEntity queryAllHistoryQuestionnaire(@RequestBody HashMap<String, Object> map) {
    HttpResponseEntity httpResponseEntity = new HttpResponseEntity();
    List<Map<String, Object>> questionnaire = questionnaireService.queryHistoryQuestionnaire(
      map);
    try {
      httpResponseEntity.setCode(Constans.SUCCESS_CODE);
      httpResponseEntity.setData(questionnaire);
      httpResponseEntity.setMessage(Constans.STATUS_MESSAGE);
    } catch (Exception e) {
      logger.info(
        "queryAllHistoryQuestionnaire 查询所有历史问卷>>>>>>>>>>>" + e.getLocalizedMessage());
      httpResponseEntity.setCode(Constans.LOGOUT_NO_CODE);
      httpResponseEntity.setMessage(Constans.EXIST_MESSAGE);
    }
    return httpResponseEntity;
  }
}
